#include <Arduino.h>
#include <esp_now.h>
#include <WiFi.h>
//  int channel[8]={0,1,2,3,4,5,6,7};
//  int pwm[8]={34,35,32,33,5,18,19,21};
//定义一个消息体，和发送端一样
typedef struct struct_message {
  int move_date;
  int isOpen;
} struct_message;
void car_move(bool move_state,int car_pwm);
void zy_move(bool move_state,int car_pwm);
void zy_ud_move(bool move_state0 ,bool move_state,int car_pwm);
void cicrle_move(bool move_state,int car_pwm);

//回调函数，当接收端接收到消息就会执行此函数
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) 
{
  struct_message myData;
  memcpy(&myData, incomingData, sizeof(myData));
  switch (myData.move_date )
  {
  case 1:
    car_move(1,150);
    break;
  case 2:
    car_move(0,150);
    break;
  case 3:
    zy_move(0,150);
    break;
  case 4:
    zy_move(1,150);
    break;
  case 5:
  zy_ud_move(0,1,150);
    break;
  case 6:
  zy_ud_move(1,0,150);
    break;
  case 7:
  zy_ud_move(1,1,150);
    break;
  case 8:
  zy_ud_move(0,0,150);
    break;
  case 9:
  cicrle_move(1,150);
    break;
  case 10:
  cicrle_move(0,150); 
    break; 
  case 0:
    car_move(0,0);
    break;   
  default:
    break;
  }
    Serial.println(myData.move_date); //打印mac地址
  }

 

void setup()
{
  Serial.begin(115200);
  // for (int i=0;i<8;i++)
  // {
  //   ledcSetup(channel[i],2000,8);
  //   ledcAttachPin(pwm[i],channel[i]);
  // }
  pinMode(23,OUTPUT);
  pinMode(14,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(18,OUTPUT);
  ledcSetup(0,2000,8);
  ledcAttachPin(25,0);

  ledcSetup(1,2000,8);
  ledcAttachPin(26,1);

  ledcSetup(2,2000,8);
  ledcAttachPin(27,2);

  ledcSetup(3,2000,8);
  ledcAttachPin(14,3);

  ledcSetup(4,2000,8);
  ledcAttachPin(5,4);

  ledcSetup(5,2000,8);
  ledcAttachPin(18,5);

  ledcSetup(6,2000,8);
  ledcAttachPin(16,6);

  ledcSetup(7,2000,8);
  ledcAttachPin(17,7);
  
  Serial.println();

  ledcWrite(0,0);
  ledcWrite(1,0);
  ledcWrite(2,0);
  ledcWrite(3,0);
  ledcWrite(4,0);
  ledcWrite(5,0);
  ledcWrite(6,0);
  ledcWrite(7,0);
  // pinMode(12,OUTPUT);
  
  WiFi.mode(WIFI_STA);

  if (esp_now_init() != ESP_OK) {
  Serial.println("Error initializing ESP-NOW");
  return;
  }
  digitalWrite(5,LOW);
  digitalWrite(18,LOW);
  digitalWrite(23,LOW);
  digitalWrite(14,LOW);
  
}
void l1_advance(bool move_state,int car_pwm)  //前进 1   后退 0
{
  ledcWrite(3-move_state,0);
  ledcWrite(2+move_state,car_pwm);

}
void l2_advance(bool move_state,int car_pwm)
{
  ledcWrite(1-move_state,car_pwm);
  ledcWrite(0+move_state,0);
}
void r1_advance(bool move_state,int car_pwm)
{
  ledcWrite(7-move_state,car_pwm);
  ledcWrite(6+move_state,0);
}
void r2_advance(bool move_state,int car_pwm)
{
  ledcWrite(5-move_state,car_pwm);
  ledcWrite(4+move_state,0);
}
void car_move(bool move_state,int car_pwm)
{
  l1_advance(move_state,car_pwm);
  l2_advance(move_state,car_pwm);
  r1_advance(move_state,car_pwm);
  r2_advance(move_state,car_pwm);
}
void zy_move(bool move_state,int car_pwm)
{
  l1_advance(1-move_state,car_pwm);
  r2_advance(1-move_state,car_pwm);
  l2_advance(move_state,car_pwm);
  r1_advance(move_state,car_pwm);
}
void cicrle_move(bool move_state,int car_pwm)
{
  l1_advance(1-move_state,car_pwm);
  l2_advance(1-move_state,car_pwm);
  r2_advance(move_state,car_pwm);
  r1_advance(move_state,car_pwm); 
}

void zy_ud_move(bool move_state0,bool move_state,int car_pwm)
{
  if (!move_state0)
  {
  l1_advance(move_state,car_pwm);
  r2_advance(move_state,car_pwm); 
  }
  else
  {
  l2_advance(move_state,car_pwm);
  r1_advance(move_state,car_pwm);
  }

}

void loop()
{
  delay(100);
  esp_now_register_recv_cb(OnDataRecv);
  Serial.print("mac Address: ");
  //  Serial.print("xyz");
  // Serial.println(WiFi.macAddress()); //打印mac地址
  
}